package com.example.fangqidian;

import android.content.res.Configuration;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.example.fangqidian.fragment.index.BookcaseFragment;
import com.example.fangqidian.fragment.index.BookstoreFragment;
import com.example.fangqidian.fragment.index.FindFragment;
import com.example.fangqidian.fragment.index.UsercenterFragment;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends BaseActivity {
    private static final String TAG = "IndexActivity";
    private TabLayout mTabLayout;
    private List<FooterModel> mFooterModels;
    private Fragment curentFragment;
    private int page=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        if(savedInstanceState!=null){
            page=savedInstanceState.getInt("page");
            TabLayout.Tab tabAt = mTabLayout.getTabAt(page);
            assert tabAt != null;
            tabAt.select();
        }else{
            setCurrentFragment(0);
        }

    }

    @Override
    void initView() {
        mTabLayout=findViewById(R.id.tabLayout);
    }

    @Override
    void initData() {
        mFooterModels=new ArrayList<>();
        mFooterModels.add(new FooterModel("书架",R.drawable.icon_bookcase));
        mFooterModels.add(new FooterModel("精选",R.drawable.icon_bookstore));
        mFooterModels.add(new FooterModel("发现",R.drawable.icon_find));
        mFooterModels.add(new FooterModel("我",R.drawable.icon_usercenter));
    }

    @Override
    void bindData() {
        for(int i=0;i<mFooterModels.size();i++){
            FooterModel model=mFooterModels.get(i);
            mTabLayout.addTab(mTabLayout.newTab().setText(model.text).setIcon(model.iconId));
        }
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.i(TAG, "tab"+tab.getPosition()+"不选中->选中");


                setCurrentFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.i(TAG, "tab"+tab.getPosition()+"选中->不选中 ");

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.i(TAG, "tab"+tab.getPosition()+"选中->选中");

            }
        });
    }

    //首页底部导航栏对象model
    private class FooterModel{
        private String text;
        private @DrawableRes int iconId;

        private FooterModel(String text, int iconId) {
            this.text = text;
            this.iconId = iconId;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("page",page);

    }

    /**
     * 设置当前显示的Fragment
     * @param position
     */
    private void setCurrentFragment(int position){
        Log.i(TAG, "setCurrentFragment: position="+position);
        FragmentManager manager = getSupportFragmentManager();
        Fragment newFragment=null;
        switch (position){
            case 0:
                newFragment=manager.findFragmentByTag(BookcaseFragment.class.getSimpleName());
                if(newFragment==null){
                    newFragment=new BookcaseFragment();
                }
                break;
            case 1:
                newFragment=manager.findFragmentByTag(BookstoreFragment.class.getSimpleName());
                if(newFragment==null){
                    newFragment=new BookstoreFragment();
                }
                break;
            case 2:
                newFragment=manager.findFragmentByTag(FindFragment.class.getSimpleName());
                if(newFragment==null){
                    newFragment=new FindFragment();
                }
                break;
            case 3:
                newFragment=manager.findFragmentByTag(UsercenterFragment.class.getSimpleName());
                if(newFragment==null){
                    newFragment=new UsercenterFragment();
                }
                break;

        }
        FragmentTransaction ft = manager.beginTransaction();
        if(newFragment==null){
            return;
        }
        //先隐藏当前显示的Fragment
        if(curentFragment!=null && !curentFragment.isHidden()){
            ft.hide(curentFragment);
        }
        if(newFragment.isAdded()){
            ft.show(newFragment);
        }else{
            ft.add(R.id.frameLayout,newFragment,newFragment.getClass().getSimpleName());
        }

        ft.commit();
        curentFragment=newFragment;
        page=position;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.i(TAG, "onConfigurationChanged: ");

        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy: ");


        super.onDestroy();
    }
}
